package com.gy.smart.tool;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;

import com.google.gson.reflect.TypeToken;
import com.gy.smart.R;
import com.gy.smart.base.BaseBean;
import com.gy.smart.base.BaseFragment;
import com.gy.smart.base.WebViewActivity;
import com.gy.smart.tool.adapter.ToolAdapter;
import com.gy.smart.tool.bean.ToolBean;
import com.gy.smart.util.AnimUtil;
import com.gy.smart.util.ListViewItemListener;
import com.gy.smart.util.RequestResultListener;
import com.gy.smart.util.Urls;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.xutils.http.RequestParams;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 高岳 on 2016/12/25.
 * Describe:工具首页
 */
public class ToolFragment extends BaseFragment implements ListViewItemListener{

    private ListView listView;
    private ToolAdapter adapter;
    private List<ToolBean> datas,requestList;

    @Override
    protected View setView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.fragment_tool,null);
        setTitleMsg("工具助手");
        _baseBack.setVisibility(View.INVISIBLE);
        return view;
    }

    @Override
    protected void initView() {
        listView = (ListView) _rootView.findViewById(R.id.fg_tool_listview);
    }

    @Override
    protected void initData() {
        datas = new ArrayList<>();
        requestList = new ArrayList<>();
//        ToolBean bean = new ToolBean();
//        bean.imgRes = R.mipmap.ic_tool_traffic;
//        bean.title = "交通出行";
//        bean.datas.add((new ToolBean()).new ToolChildBean("车辆违法","https://www.baidu.com"));
//        bean.datas.add((new ToolBean()).new ToolChildBean("路况信息","https://www.baidu.com"));
//        bean.datas.add((new ToolBean()).new ToolChildBean("地铁线路","https://www.baidu.com"));
//        bean.datas.add((new ToolBean()).new ToolChildBean("区内交通","https://www.baidu.com"));
//        bean.datas.add((new ToolBean()).new ToolChildBean("航班时刻表","https://www.baidu.com"));
//        datas.add(bean);
//        bean = new ToolBean();
//        bean.imgRes = R.mipmap.ic_tool_traffic;
//        bean.title = "环境气象";
//        bean.datas.add((new ToolBean()).new ToolChildBean("空气质量","https://www.baidu.com"));
//        bean.datas.add((new ToolBean()).new ToolChildBean("天气预报","https://www.baidu.com"));
//        bean.datas.add((new ToolBean()).new ToolChildBean("地质气象预报","https://www.baidu.com"));
//        datas.add(bean);
        adapter = new ToolAdapter(mContext,datas);
        adapter.setListener(this);
    }

    @Override
    protected void setListener() {
        super.setListener();
    }

    @Override
    protected void fillData() {
        super.fillData();
        listView.setAdapter(adapter);
        getTools();
    }

    @Override
    public void doPassActionListener(Object obj, int org1, int org2, String str) {
        Intent intent = new Intent(mContext, WebViewActivity.class);
        intent.putExtra(WebViewActivity.URL,datas.get(org1).toolData.get(org2).url);
        startActivity(intent);
        AnimUtil.pushLeftInAndOut(getActivity());
    }

    private void getTools(){
        showLoadDialog(1);
        RequestParams params = new RequestParams(Urls.url_tool_list);
        postRequest(params, new RequestResultListener() {
            @Override
            public boolean onSuccess(BaseBean result) {
                if(result.status == 1){
                    Type type = new TypeToken<List<ToolBean>>(){}.getType();
                    requestList = gson.fromJson(result.data,type);
                    datas.addAll(requestList);
                    adapter.notifyDataSetChanged();
                }
                return false;
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }
        });
    }
}
